import {msgLogin as login ,sendMsg} from '../../api/user'
import { hexMD5 } from "../../utils/md5.js"
const app = getApp()

Page({
    data: {
        disabled: true,
        btnstate: 'default',
        account: '',
        password: '',
        showPassword: true,
        verifyCode:'',
        verifyCodeKey:'',
        isShow: true,
        second: 60,
        verifyCode:''
    },
    verificationClick() {
        if (this.data.account == "" || this.data.account.length != 11 || !(/^((1))\d{10}$/.test(this.data.account))) {
          wx.showToast({
            title: '请输入正确手机号码',
            icon: 'none',
            duration: 2000
          })
          return;
        }
        this.timer()
        sendMsg({
          username: this.data.account,
                tplKey: 'register',
          mchId: app.ISV_ID,
        }).then(res => {
    
        })
        this.data.isShow = false
        this.setData({
          isShow: this.data.isShow
        })
    },
    verifyCodeChange(e) {
        this.data.verifyCode = e.detail.value
    },
    timer: function () {
        let promise = new Promise((resolve, reject) => {
        let setTimer = setInterval(
            () => {
            this.setData({
                second: this.data.second - 1
            })
            if (this.data.second <= 0) {
                this.setData({
                second: 60,
                isShow: true,
                })
                resolve(setTimer)
            }
            }, 1000)
        })
        promise.then((setTimer) => {
        clearInterval(setTimer)
        })
    },
    login(){
        let username = 'mch_'+ app.globalData.mchId +'_'+ this.data.account.trim()
        let params={
            username: username,
            password: hexMD5(this.data.password),
            verifyCode: this.data.verifyCode,
            mchId:'10200'
        }
        login(params).then(res => {
            console.log(res)
            if (res.type == 'success') {
                const {
                  userInfo,
                  token
                } = res.data
                    wx.setStorageSync('token', token);
                    app.globalData.token = token
                     app.globalData.userInfo = userInfo
                wx.switchTab({
                  url: '/pages/my/my'
                })
            }
        })
    },
    showPasswordClick(){
        this.data.showPassword = ! this.data.showPassword
        this.setData({
            showPassword: this.data.showPassword
        });
    },
    accountInput (event) {
        this.data.account = event.detail.value.trim();
        this.setData({
            account:this.data.account
        })
    },
    pwdBlur: function(e) {
        var password = e.detail.value;
        if (password != '') {
            this.setData({
                password: password
            });
        }
        if (password !== '') {
            this.setData({
                disabled: false,
                btnstate: 'primary'
            });
        } else {
            this.setData({
                disabled: true,
                btnstate: 'default'
            });
        }
    },
    LogAndReg(){
        wx.navigateTo({
            url: '/pages/safety/msgLogin'
        })
    }
})
